﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.Form;
using PMPUtils.Maintenance;
using PMPUtils.Resources;

namespace PMP.Maintenance
{
    public partial class SprintMaintenance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.AuthenticateLead(Session, Response);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            // Check required fields
            if (!CheckRequiredFields())
            {
                Messenger.Show(Response, "Please fill required fields then proceed!");
                return;
            }

            String sprint = txtSprint.Text.Trim();
            String sprintStart = txtSprintStart.Text.Trim();
            String NF1Capacity = txtNF1Capacity.Text.Trim();
            String NF2Capacity = txtNF2Capacity.Text.Trim();
            String NF1CapacityNotes = txtNF1CapacityNotes.Text.Trim();
            String NF2CapacityNotes = txtNF2CapacityNotes.Text.Trim();

            // Validate exist
            try
            {
                if (SprintMaintain.IsSprintAlreadyExists(sprint))
                {
                    Messenger.Show(Response, "Sprint name already exists!");
                    return;
                }
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            // Add a new sprint
            try
            {
                SprintMaintain.AddSprint(sprint, sprintStart, NF1Capacity, NF2Capacity, NF1CapacityNotes, NF2CapacityNotes);
            }
            catch (Exception ex)
            {
                Messenger.Show(Response, "Error: " + ex.Message);
                return;
            }

            grvSprints.DataBind();
            CleanUp();

            Messenger.Show(Response, sprint + " Added Successfully!");
        }

        private bool CheckRequiredFields()
        {
            if (txtSprint.Text.Trim().Equals(String.Empty) ||
                txtSprintStart.Text.Trim().Equals(String.Empty) ||
                txtNF1Capacity.Text.Trim().Equals(String.Empty) ||
                txtNF2Capacity.Text.Trim().Equals(String.Empty))
            {
                return false;
            }

            return true;
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            CleanUp();
        }

        private void CleanUp()
        {
            txtSprint.Text = String.Empty;
            txtSprintStart.Text = String.Empty;
            txtNF1Capacity.Text = String.Empty;
            txtNF2Capacity.Text = String.Empty;
            txtNF1CapacityNotes.Text = String.Empty;
            txtNF2CapacityNotes.Text = String.Empty;
        }
    }
}
